<?php
/* @var $this InvoiceController */
/* @var $form CActiveForm */
?>

<?php
$this->breadcrumbs=array(
	'Rechnungen'=>array('invoice/admin'),
	'Neue Rechnung',
);

setlocale(LC_ALL, "de_DE.UTF-8");
?>

<br><br><br>
<h3 class="error">Bitte Kunde wählen:</h3>

<div class="form">

    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'person-form-edit_person-form',
        'enableAjaxValidation'=>false,
        'htmlOptions'=>array(
            'onsubmit'=>"return false;",/* Disable normal form submit */
            'onkeypress'=>" if(event.keyCode == 13){ send(); } ", /* Do ajax call when user presses enter key */
        ),
    )); ?>

        <?php $this->widget('ext.chosen.Chosen',array(
            'name' => 'customer_id', // input name
            //TODO 'value' => ,
            'data' => $this->getCustomerArray(),
            'htmlOptions'=>array(
                'onchange'=>'send();',
                'prompt' => '-- Kunde wählen --'
            )
        ));?>
        <div id="data">

        </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->

<script type="text/javascript">

    function send() {

        var data=$("#person-form-edit_person-form").serialize();

        $.ajax({
            type: 'POST',
            url: '<?php echo Yii::app()->createAbsoluteUrl("invoice/ajax"); ?>',
            data: data,
            success: function(data){
                //alert(data);
                var ni = document.getElementById('data');

                if (document.getElementById("myDiv")) {
                    var element = document.getElementById("myDiv");
                    element.parentNode.removeChild(element);
                }

                var newdiv = document.createElement('div');

                var divIdName = 'myDiv';

                newdiv.setAttribute('id', divIdName);

                newdiv.innerHTML = data;

                ni.appendChild(newdiv);
            },
            error: function(data) { // if error occured
                alert("Error occured.please try again");
                alert(data);
            },

            dataType:'html'
        });

    }

</script>

<script type="text/javascript">

    function sendAddress() {

        var data=$("#address-form").serialize();

        $.ajax({
            type: 'POST',
            url: '<?php echo Yii::app()->createAbsoluteUrl("invoice/ajaxAddressSelected"); ?>',
            data: data
        });
    }

</script>

<br><br>

<?php
echo CHtml::button("3. Rechnung erstellen", array("submit" => array("invoice/createAndSaveInvoice")));
?>



